package com.dongdongshop.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.pojo.TbSeller;
import com.dongdong.service.LoginService;
import com.dongdongshop.exception.LonginException;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;


public class LoginRealm extends AuthorizingRealm {

//
    @Reference
    private LoginService loginService;


//      我们编写授权逻辑的代码
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权方法....");
        return null;
    }

//    我们编写认证逻辑的方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认真方法 ... 登录");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        TbSeller tbSeller =  loginService.selectNa(token.getUsername());
        if(tbSeller == null){
            return null;
        }

        if(!("1".equals(tbSeller.getStatus()))){
            throw new LonginException("无法登录!!!账号异常【可能未审核,或账号已封】");
        }

        return new SimpleAuthenticationInfo(tbSeller,tbSeller.getPassword(), ByteSource.Util.bytes(tbSeller.getSalt()),tbSeller.getLname());
    }
}
